<?php
abstract class ImageThumnailHelper
{
	   //图片类型
    var $type;
    //实际宽度
    var $width;
    //实际高度
    var $height;
    //改变后的宽度
    var $resize_width;
    //临时文件宽度
    var $target_width;
    //改变后的高度
    var $resize_height;
    //临时文件高度
    var $target_height;
    //源图象
    var $srcimg;
    //目标图象地址
    var $dstimg;
    //临时创建的图象
    var $im;

    function resizeimage($srcpath,$dstpath, $width=false, $height=false)
    {
        $this->srcimg = $srcpath;
        if($width)
       		$this->resize_width = $width;
       	if($height)
        $this->resize_height = $height;
        //图片的类型
    
				$this->type = strtolower(substr(strrchr($this->srcimg,"."),1));

        //初始化图象
        $this->initi_img();
        //目标图象地址
        $this -> dst_img($dstpath);
        //--
        $this->width = imagesx($this->im);
        $this->height = imagesy($this->im);
        //生成图象
        $this->newimg();
        ImageDestroy ($this->im);
    }
    abstract function newimg();
    //初始化图象
    function initi_img()
    {
   		 @ini_set('memory_limit', '256M');
    
        if($this->type=="jpg" || $this->type=="jpeg")
        {
            $this->im = imagecreatefromjpeg($this->srcimg);
        }
        if($this->type=="gif")
        {
            $this->im = imagecreatefromgif($this->srcimg);
        }
        if($this->type=="png")
        {
            $this->im = imagecreatefrompng($this->srcimg);
        }
        
       // @ini_set('memory_limit', '256M');
		//$this->im = imagecreatefromstring( file_get_contents($this->srcimg) );
    }
    //图象目标地址
    function dst_img($dstpath)
    {
        $full_length  = strlen($this->srcimg);
        $type_length  = strlen($this->type);
        $name_length  = $full_length-$type_length;

        $name         = substr($this->srcimg,0,$name_length-1);
        $this->dstimg = $dstpath;
    }
}


class ImageThumnailHelper_FixWidthAutoHeight extends ImageThumnailHelper
{
	//override newimage function
	function newimg()
	{
				//Calculate resie image height
				$scale = ($this->resize_width)/($this->width);
				$this->target_height = $this->height*$scale;
				if($this->target_height < 5)
					$this->target_height = 5;
					
			  $newimg = imagecreatetruecolor($this->resize_width,$this->target_height);
        imagecopyresampled($newimg, $this->im, 0, 0, 0, 0, $this->resize_width,$this->target_height, $this->width, $this->height);
        ImageJpeg ($newimg,$this->dstimg);

	}
}
class ImageThumnailHelper_FixWidthAutoHeight_cutHeight extends ImageThumnailHelper_FixWidthAutoHeight
{
	//override newimage function
	function newimg()
	{
		 parent::newimg();
		//cut width image
		 if($this->target_height > $this->resize_height)
		 {
		 		 //move dest image as source image
				 $this->srcimg = $this->dstimg.'.tmp.jpg';
				 rename($this->dstimg, $this->srcimg);
				 $this->initi_img();

		 		$cut_length = $this->target_height - $this->resize_height;
		 		$newimg = imagecreatetruecolor($this->resize_width,$this->resize_height);
        imagecopyresampled($newimg, $this->im, 0, 0, 0, $cut_length/2, $this->resize_width,$this->resize_height,$this->resize_width,$this->target_height - $cut_length);
        ImageJpeg ($newimg,$this->dstimg);
				@unlink($this->srcimg);
		 }	 
		 
	}
}


class ImageThumnailHelper_FixHeightAutoWidth extends ImageThumnailHelper
{
	//override newimage function
	function newimg()
	{
				//Calculate resie image width
				$scale = ($this->resize_height)/($this->height);
				$this->target_width = $this->width*$scale;
				
			  $newimg = imagecreatetruecolor($this->target_width,$this->resize_height);
        imagecopyresampled($newimg, $this->im, 0, 0, 0, 0, $this->target_width,$this->resize_height, $this->width, $this->height);
        ImageJpeg ($newimg,$this->dstimg);

	}
}

class ImageThumnailHelper_FixHeightAutoWidth_cutWidth extends ImageThumnailHelper_FixHeightAutoWidth
{
	//override newimage function
	function newimg()
	{
		 parent::newimg();
		//cut width image
		 if($this->target_width > $this->resize_width)
		 {
		 		 //move dest image as source image
				 $this->srcimg = $this->dstimg.'.tmp.jpg';
				 rename($this->dstimg, $this->srcimg);
				 $this->initi_img();
		 		$cut_length = $this->target_width - $this->resize_height;
		 		$newimg = imagecreatetruecolor($this->resize_width,$this->resize_height);
		 		
        imagecopyresampled($newimg, $this->im, 0,0,  $cut_length/2, 0, $this->resize_width,$this->resize_height,$this->resize_width,$this->resize_height);
        ImageJpeg ($newimg,$this->dstimg);
				@unlink($this->srcimg);
		 }	 
		 
	}
}

class ImageThumnailHelper_Auto 
{
	function resizeimage($src,$des,$width,$height)
	{
		list($src_width, $src_height, $type, $attr) = getimagesize($src);
		if($src_width > $src_height)
			$helper = new ImageThumnailHelper_FixHeightAutoWidth_cutWidth();
		else
			$helper = new ImageThumnailHelper_FixWidthAutoHeight_cutHeight();
		
		$helper->resizeimage($src,$des,$width,$height);
	}
}

class ImageThumnailHelper_Crop
{
 	var $type;
 	var $im;
 	var $srcimg;
 	
	public function crop($src,$desc,$width,$height,$c_x,$c_y,$c_width,$c_height)
	{
		$this->srcimg = $src;
		$this->type = strtolower(substr(strrchr($this->srcimg,"."),1));
		$this->initi_img();
		$newimg = imagecreatetruecolor($width,$height);
	
        imagecopyresampled($newimg, $this->im,0,0, $c_x,$c_y,$width,$height, $c_width,$c_height);
        ImageJpeg ($newimg,$desc);
        
        ImageDestroy ($newimg);
        ImageDestroy ($this->im);
	}
	function initi_img()
    {
   		 @ini_set('memory_limit', '256M');
    
        if($this->type=="jpg" || $this->type=="jpeg")
        {
            $this->im = imagecreatefromjpeg($this->srcimg);
        }
        if($this->type=="gif")
        {
            $this->im = imagecreatefromgif($this->srcimg);
        }
        if($this->type=="png")
        {
            $this->im = imagecreatefrompng($this->srcimg);
        }
    }
}

/**
/*$mode
			0 : Fixed width auto height
			1 : Fixed height auto with
			2 : Fixe width then cut height
			3 : Fixed height then cut with
*/
function make_thumnail($src,$des,$width,$height,$mode = 0)
{
	switch($mode)
	{
		case '0':
		$helper = new ImageThumnailHelper_FixWidthAutoHeight();
		break;
		case '1':
		$helper = new ImageThumnailHelper_FixHeightAutoWidth();
		break;
		case '2':
		$helper = new ImageThumnailHelper_FixWidthAutoHeight_cutHeight();
		break;
		case '3':
		$helper = new ImageThumnailHelper_FixHeightAutoWidth_cutWidth();
		break;
		case '4':
		$helper = new ImageThumnailHelper_Auto();
		break;
	}
  $helper->resizeimage($src,$des,$width,$height);
}
?>